<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

    <title>Troy.Chen--管理系统--日志查询</title>
    <link rel="stylesheet" type="text/css" href="style/public.css"/>
    <script charset="utf-8" type="text/javascript" src="/js/base64.js"></script>
    <script charset="utf-8" type="text/javascript" src="/js/ajax-1.1.js"></script>
    <script charset="utf-8" type="text/javascript" src="js/query.js"></script>
    <script type="application/javascript">
        var q = null;
        window.onload = async function () {
            await Ajax.post("/query/allDataName", "",
                (response) => {
                    let responseText = response.responseText;
                    let evalJson = JSON.parse(responseText);
                    if (evalJson.code !== 0) {
                        alert("code：" + evalJson.code + ", message：" + evalJson.msg);
                        if (evalJson.code === 99) {
                            top.location.href = "login.html";
                        }
                    } else {
                        for (let index in evalJson.data) {
                            let li = document.createElement("li");
                            li.id = "li_" + index;
                            li.setAttribute("title", evalJson.data[index] + "(" + index + ")");
                            li.setAttribute("onclick", "changeLog(\"" + index + "\")");
                            li.innerText = evalJson.data[index];
                            $("ul_log_title").appendChild(li);
                        }
                    }
                },
                (error) => {
                    alert(error);
                }
            );

            let tableName = getQueryVariable("tableName");
            if (!isNull(tableName)) {
                q = new QueryInfo();
                q.tableName = tableName;
                q.pageSize = getQueryVariable("pageSize");
                q.pageIndex = getQueryVariable("pageIndex");
                let queryWhere = getQueryVariable("queryWhere");
                if (!isNull(queryWhere)) {
                    let decode = Base64.decode(queryWhere);
                    q.queryWhere = JSON.parse(decode);
                }
                q.init();
                $("li_" + tableName).className = "hover";
            }
            $hide("div_loading");
        }

        /*切换查询*/
        function changeLog(tableName) {
            q = new QueryInfo();
            q.tableName = tableName;
            q.init();
        }

        function changePage(change) {
            q.changePage(change);
        }

        function addQueryWhere() {
            let key = getSelectValue("sel_key");
            let sel_query_Key = getSelectValue("sel_query_Key");
            let value = $("txt_query").value;
            if (!isNull(value) || "$group" === sel_query_Key) {
                var oldValue = q.queryWhere[key];
                switch (sel_query_Key) {
                    case "=": {
                        oldValue = value;
                    }
                        break;
                    case "$gt":
                    case "$gte":
                    case "$lt":
                    case "$lte": {
                        if (!(oldValue instanceof Object)) {
                            oldValue = {};
                        }
                        oldValue[sel_query_Key] = value;
                    }
                        break;
                    case "$in":
                    case "$nin": {
                        oldValue = {};
                        oldValue[sel_query_Key] = value.split(",")
                    }
                        break;
                    case "/": {
                        oldValue = "^.*" + value + ".*$";
                    }
                        break;
                    case "$group": {
                        sel_query_Key = key;
                        key = "$group";
                        oldValue = q.queryWhere[key];
                        if (!(oldValue instanceof Object)) {
                            oldValue = {};
                        }
                        oldValue[sel_query_Key] = "$" + sel_query_Key;
                    }
                        break;
                    case "$or": {
                        sel_query_Key = key;
                        key = "$or";
                        oldValue = q.queryWhere[key];
                        if (!(oldValue instanceof Object)) {
                            oldValue = {};
                        }
                        oldValue[sel_query_Key] = value;
                    }
                        break;
                    default:
                        alert("无法识别！" + sel_query_Key);
                        break;
                }
                q.queryWhere[key] = oldValue;
                $('txt_query_where').value = JSON.stringify(q.queryWhere);
            } else {
                alert("请设置条件");
                $("txt_query").focus();
            }
            $("txt_query").value = "";
        }

        function queryWhere() {
            q.pageIndex = 1;
            q.init();
        }

        function reset() {
            $('txt_query_where').value = '';
            q.queryWhere = {};
            q.init();
        }

        function outExcel() {
            if (isNull(q) || isNull(q.dataCount) || q.dataCount < 1) {
                alert("查询结果没有数据！");
                return;
            }
            q.outExcel();
        }
    </script>
</head>
<body>
<div class="div_body_content">
    <div class="div_page">
        &nbsp;
        <span>分页：</span>
        <span>
                <input type="button" value="<<" title="首页" onclick="changePage('<<')">
            <input type="button" value="<" title="上一页" onclick="changePage('<')">
            <input id="txt_page_index" size="6" style="text-align: center" type="text" value="0">
            <input type="button" value=">" title="下一页" onclick="changePage('>')">
            <input type="button" value=">>" title="尾页" onclick="changePage('>>')">
            </span>
        &nbsp;&nbsp;共&nbsp;<span id="span_data_count"></span>&nbsp;条&nbsp;&nbsp; 每页&nbsp;
        <input id="txt_page_size" size="4" style="text-align: center" type="text"
               value="15"
               title="参数最小10条，最大1000条">
        &nbsp;条&nbsp;&nbsp;共&nbsp;<span id="span_page_max"></span>&nbsp;页
        &nbsp;<a href="javascript:void(0);" onclick="outExcel();">导出Excel</a>
    </div>
    <div id="div_data" class="div_data">

    </div>
</div>
</body>
</html>